var pageSize = 20;
var pageStart = 1;
$(function(){
	var id = $("#nav").val();
	pageStart = $("#page").val();
	loadArticle(id);
});

function loadArticle(id){
	$.ajax({
		url:BASE_PATH+'/index/get.do',
		type:'get',
		dataType:'json',
		data:{
			nav:id,
			pageSize:pageSize,
			pageStart:pageStart
		},
		success:function(data){
			createArticle(data);
			//一页
			if(data.pageCount<=1){
				$("#pageCount").html("");
				$("#currentPage").html("");
				$("#middle").html("");
				$("#prevPage").html("");
				$("#nextPage").html("");
			}else{
				$("#pageCount").html("共"+data.pageCount+"页");
				$("#middle").html("/");
				$("#currentPage").html("第"+data.page+"页");
				$("#prevPage").html("<a href='"+(data.page==1?"#":BASE_PATH+"/"+id+"/"+(Number(data.page)-1)+"/writings.html")+"'>上一页</a>");
				$("#nextPage").html("<a href='"+(data.page==data.pageCount?"#":BASE_PATH+"/"+id+"/"+(Number(data.page)+1)+"/writings.html")+"'>下一页</a>");
				if(data.page==1){
					//第一页
					$("#prevPage").attr("disabled",true);
					$("#nextPage").removeAttr("disabled");
				}else if(data.page==data.pageCount){
					//最后一页
					$("#nextPage").attr("disabled",true);
					$("#prevPage").removeAttr("disabled");
				}else{
					$("#prevPage").removeAttr("disabled");
					$("#nextPage").removeAttr("disabled");
				}
			}
			
			
		}
	});
}

function createArticle(data){
	var html = "";
	if(data){
		for(var title in data){
			if(title != 'nav' && title != 'tag' && title != 'pageCount' && title != 'page'){
				html+="<div class='row title_bottom'><h3 class='pull-left'>"+data.nav[title].title+"</h3><div class='pull-right more'></div> </div>";
				for(var i =0 ; i < data[title].length;i++){
					if(i == data[title].length-1){
						html+="<div class='row title-conatiner-end'> <div class='col-md-3 col-sm-12 title-img hidden-xs'> <a href='"+BASE_PATH+"/article/"+data[title][i].id+".html'><img src='"+BASE_PATH+"/"+data[title][i].thumbnail+"'></a> </div> <div class='col-md-9 col-sm-12  title-content'> <ul> <a href='"+BASE_PATH+"/article/"+data[title][i].id+".html'><h3>"+cutWords(26,data[title][i].title)+"</h3></a> <p>"+cutWords(150,data[title][i].description)+"</p> <p class='title-info'> <span class='tag'>"+createTag(data[title][i].tag,data.tag)+"</span> <span class='time'>"+formateDate(data[title][i].updateTime)+"</span> <span class='comment'></span> <span class='read-count'>阅读("+data[title][i].readCount+")</span> </p> </ul> </div> </div>";
					}else{
						html+="<div class='row title-conatiner'> <div class='col-md-3 col-sm-12 title-img hidden-xs'> <a href='"+BASE_PATH+"/article/"+data[title][i].id+".html'><img src='"+BASE_PATH+"/"+data[title][i].thumbnail+"'></a> </div> <div class='col-md-9 col-sm-12  title-content'> <ul> <a href='"+BASE_PATH+"/article/"+data[title][i].id+".html'><h3>"+cutWords(26,data[title][i].title)+"</h3></a> <p>"+cutWords(150,data[title][i].description)+"</p> <p class='title-info'> <span class='tag'>"+createTag(data[title][i].tag,data.tag)+"</span> <span class='time'>"+formateDate(data[title][i].updateTime)+"</span> <span class='comment'></span> <span class='read-count'>阅读("+data[title][i].readCount+")</span> </p> </ul> </div> </div>";
					}
				}
			}
		}
	}
	$("#article").html(html);
}
